//引用axios所需要组件
import axios from 'axios'
import {developUrl,serverDK,serverNumberPork} from './api';

// 智能数据交换云平台地址
export const dataExchangeHost = developUrl+serverDK;

//封装axios
export const axiosDataFun = (method,url,dataParam) => {
    return axios({
        headers: {
            'Content-Type': 'application/json'
        },
        method: method,
        url: url,
        data: dataParam
    }).then(res => res).catch( error => error )
}

//组织信息列表添加
export const organizationDataExchangeAdd = (param) => {
    return axiosDataFun('post', dataExchangeHost+'organization/save', param).then(res => res);
}

//组织信息列表删除
export const organizationDataExchangeDel = (param,id) => {
    return axiosDataFun('get', dataExchangeHost+'organization/'+id+'/del', param).then(res => res);
}

//组织信息列表和查询
export const organizationDataExchangeListSearch = (param) => {
    return axiosDataFun('post', dataExchangeHost+'organization/search', param).then(res => res);
}
export const organizationListDisplay = (param,ev) =>{
    return organizationDataExchangeListSearch(param).then(res => {
        ev.pageTotal = res.data.data.total
        const result = res.data.data.list;
        ev.tableData = [];
        organizationDataExchangeQuyu({}).then(res =>{   //查询区域
            let checkListQuyuList = res.data.data.list;
            console.log(checkListQuyuList);
            for(let i=0 ; i < result.length; i++ ){
                let obj = {};
                let resultType = '';
                let resultState = '';
                 if(result[i].requesterType == 1){
                    resultType = '请求方'
                }else if(result[i].producerType == 1){
                    resultType = '服务方'
                }
                checkListQuyuList.map(res=>{
                    if(res.id === result[i].nodeIds){
                        obj.area = res.name;    //区域键值转换
                    }
                })
                resultState = result[i].status ? '启用' : '禁用';
                obj.code = result[i].id;
                obj.flowNum = result[i].flowNum;
                obj.oragion = result[i].name;
                obj.type = resultType;
                obj.state = resultState;
                ev.tableData.push(obj);
            } 
        }).catch(error =>{
            console.log(error);
        });
        
    }).catch(error => {
        console.log(error);
    })
}

//组织信息列表详细内容
export const organizationDataExchangeListDetail = (param,id) => {
    return axiosDataFun('get', dataExchangeHost+'organization/detail/'+id, param).then(res => res);
}

//组织信息列表更新
export const organizationDataExchangeUpdata = (param) => {
    return axiosDataFun('post', dataExchangeHost+'organization/update', param).then(res => res);
}

//组织信息区域列表
export const organizationDataExchangeQuyu = (param) => {
    return axiosDataFun('post',dataExchangeHost+'area/search',param).then(res => res);
}


//组织部门列表
export const organizationList = (param) => {
    return axiosDataFun('post', developUrl+serverNumberPork+'/uaa/organization/search', param).then(res => res);
}
